/**
 * Created by Administrator on 2016/1/26.
 */

require.config(require_config);
define([
    'jquery',
    'common',
    'ready',
    'template',
    'collection',
    'model',
    'dialog'
],function($, common, ready, template, Collection, Model, dialog){
    var id = getUrlPath(3);
    var index = function () {
        this.init();
    }

    index.prototype = {
        init: function () {
            var _this = this;
            //所有规格
            $.ajax({
                url: _global.url.api + 'get_specification',
                data: {id: id,isGroup:true},
                type: 'get',
                async: false,
                success: function (res) {
                    if (res.code == 0) {
                        _this.set('spec_list', res.data.spec_list);
                        _this.set('all_spec',res.data.mySpec);
                    }
                }
            });
            this.fetch();
            this.event();
        },

        fetch: function () {
            var _this = this;
            $('#addForm').html(template('add-tpl',{
                spec_list : this.spec_list,
            }));

            $('#all_spec').html(template('list-tpl',{
                all_spec : this.all_spec,
            }));

            this.model = new Model({
                //url: _global.url.api + 'site',
                data: {}
            });

        },

        event: function () {
            var _this = this;
            $('#add-item').on('click', function () {
                _this.model.url = _global.url.api + 'set_specification',
                _this.model.data.spec = $('input[name="specification_id"]').val();
                _this.model.data.cid = id;
                if(_this.model.post()){
                    _this.init();
                }
            });

            $('.item-remove').on('click', function () {
                _this.model.url = _global.url.api + 'set_specification',
                _this.model.data.id = $(this).find('input[name="specificationId"]').val();
                if(_this.model.delete()){
                    _this.init();
                }
            });
        },

        set: function ($k, $v) {
            this[$k] = $v;
        },
    }

    new index();
});
